package com.ice.work.moment.crawler.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.honeybee.cloud.framework.base.exception.business.BaseBizException;
import com.honeybee.cloud.framework.common.constant.CommonSystemConstant;
import com.honeybee.cloud.framework.common.enums.DataStatusEnum;
import com.honeybee.cloud.framework.common.page.Page;
import com.ice.work.moment.crawler.dao.position.type.PositionTypeRepository;
import com.ice.work.moment.crawler.service.IlPositionTypeService;
import com.ice.work.moment.facade.crawler.dto.PositionTypeDTO;
import com.ice.work.moment.facade.crawler.mapper.PositionTypeConvertMapper;
import com.ice.work.moment.facade.crawler.model.PositionType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.Collection;
import java.util.Objects;

/**
 * @Description 机构持仓类型
 * @Author DELL
 * @Create 2019-05-13
 * @Since 1.0.0
 */
@Component
public class PositionTypeServiceImpl implements IlPositionTypeService {
    @Autowired
    private PositionTypeRepository typeRepository;

    @Override
    public Boolean save(Collection<PositionType> types) throws BaseBizException {
        Assert.notEmpty(types, "types not null");
        return typeRepository.saveBatch(types, CommonSystemConstant.BATCH_SIZE);
    }

    @Override
    public Boolean save(PositionType type) throws BaseBizException {
        Assert.notNull(type, "type not null");
        return typeRepository.save(type);
    }

    @Override
    public PositionTypeDTO findByValue(String value) throws BaseBizException {
        PositionType type = typeRepository.findByValueAndDataStatus(value, DataStatusEnum.VALID.key);
        return PositionTypeConvertMapper.INSTANCE.toDTO(type);
    }

    @Override
    public Boolean saveOrUpdate(PositionType type) throws BaseBizException {
        Assert.notNull(type, "type not null");
        PositionType exists = typeRepository.findByValueAndDataStatus(type);
        if (Objects.nonNull(exists)) {
            type.setId(exists.getId());
        }
        return typeRepository.saveOrUpdate(type);
    }

    @Override
    public Page<PositionTypeDTO> page(IPage<PositionType> page) throws BaseBizException {
        Assert.notNull(page, "page not null");
        IPage<PositionType> pageInfo = typeRepository.page(page);
        return PositionTypeConvertMapper.INSTANCE.toDTO(pageInfo);
    }
}
